/**
* @description: 标准化作业--检修备件
* @author: wqn & yanxiaoyue
* @data:2023年6月15日/2023年7月14日
* @remark:
**/
<template>
	<view class="maintenance">
		<nav-bar title="检修备件" @goBack="goBack"></nav-bar>
		<view class="main">
			<view>
				<!--      全选部分-->
				<view class="title layout" @click="checkAll">
					<text>检修备件</text>
					<van-checkbox v-model="isCheckAll">
						<template #icon="props">
							<img class="img-icon" :src="props.checked ? inactiveIcon : activeIcon" />
						</template>
					</van-checkbox>
					<text :class="isCheckAll ? 'active' : ''">全选</text>
				</view>
				<!--      检修的仪器-->
				<view class="content-list" v-for="(item,index) in sparePart" :key="index">
					<view class="border">
						<view class="list-top layout" @click="checkShop(item)">
							<img style="width: 22px;height: 22px;"
								src="../../../static/standardizedOperationImg/zxckBlue.png" alt="">
							<text>{{ item.deviceClass }}</text>
							<van-checkbox v-model="item.checked">
								<template #icon="props">
									<img class="img-icon" :src="props.checked ? inactiveIcon : activeIcon" />
								</template>
							</van-checkbox>
						</view>
						<view class="list-bottom" v-for="(pro,index) in item.jxsblist" :key="index">
							<van-divider />
							<view class="details layout" @click="isChildCheck(item,pro)">
								<text>{{ pro.bjType }} ({{ pro.demandNum }})</text>
								<van-checkbox v-model="pro.isChecked">
									<template #icon="props">
										<img class="img-icon" :src="props.checked ? inactiveIcon : activeIcon" />
									</template>
								</van-checkbox>
							</view>
						</view>
					</view>
				</view>
			</view>


		</view>
		<!--      确认按钮-->
		<view class="verify">
			<van-button type="info" round @click="affirmClick">确认</van-button>
		</view>
	</view>
</template>

<script>
	import navBar from "../common/navBar.vue"
	import {
		Dialog,
		Toast
	} from "vant";
	import {
		selectjxbjoptical,
		insertjxbjopticallog
	} from "../../../api/bzhzyApi";

	export default {
		name: "index",
		components: {
			navBar
		},
		data() {
			return {
				isCheckAll: false, // 是否全选
				allCheck: 1,
				activeIcon: require('../../../static/img/no-click.png'),
				inactiveIcon: require('../../../static/img/clicked.png'),
				sparePart: [],
			}
		},
		onLoad(option) {
			this.work = JSON.parse(option.message)
			this.getJxbjoptical();
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			/**
			 * 通知上一个页面刷新数据
			 */
			back() {
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.isRefresh = true
				uni.navigateBack()
			},
			// 获取所有检修备件
			getJxbjoptical() {
				this.$apiRequest.bzhzyApi.selectjxbjoptical({
					consultationDeductionId: this.work.OBJID
				}).then(res => {
					this.sparePart = res.data;
					let allArr = [],
						CheckedAllArr = [];
					this.sparePart.forEach(function(item) {
						allArr = item.jxsblist.filter(item => item);
						CheckedAllArr = item.jxsblist.filter(item => item.isChecked == true);
					})
					this.allCheck = CheckedAllArr.length;
					this.allCheck === allArr.length ? this.isCheckAll = true : this.isCheckAll = false;
				})
			},
			isChildCheck(item, pro) {
				!pro.isChecked ? this.checkTrue(item, pro) : this.checkFalse(item, pro)
			},
			checkTrue(item, pro) {
				pro.isChecked = true;
				//标题勾选
				if (++item.checkedCount === item.jxsblist.length) {
					item.checked = true;
				} else {
					return ''
				}
				if (item.checked) {
					++this.allCheck;
					this.allCheck === this.sparePart.length ? this.isCheckAll = true : this.isCheckAll = false
				} else {
					return ''
				}
			},
			checkFalse(item, pro) {
				pro.isChecked = false;
				--item.checkedCount;
				if (item.checked) {
					item.checked = false
						--this.allCheck
					this.allCheck !== this.sparePart.length ? this.isCheckAll = false : this.isCheckAll = true
				}
			},
			//组全选
			checkShop(item) {
				!item.checked ? this.shopTrue(item) : this.shopFalse(item)
			},
			//全选已选中
			shopTrue(item) {
				item.jxsblist.forEach(pro => {
					if (pro.isChecked === false) {
						this.checkTrue(item, pro)
					} else {
						return ''
					}
				})
			},
			//全选取消选中
			shopFalse(item) {
				item.jxsblist.forEach(pro => {
					if (pro.isChecked === true) {
						this.checkFalse(item, pro)
					} else {
						return ''
					}
				})
			},
			//全选
			checkAll() {
				this.isCheckAll = !this.isCheckAll;
				this.isCheckAll ? this.sparePart.forEach((item, pro) => {
					this.shopTrue(item)
				}) : this.sparePart.forEach(item => {
					this.shopFalse(item)
				})
			},
			affirmClick() {
				let CheckedAllArr = [];
				this.sparePart.forEach(function(item) {
					item.jxsblist.filter(function(pro) {
						if (pro.isChecked === true) {
							CheckedAllArr.push(pro.objId)
						}
					});
				})
				Dialog.confirm({
						message: '提交后不可修改，是否确定提交？',
						confirmButtonColor: "#417adc",
					})
					.then(() => {
						// on confirm
						this.$apiRequest.bzhzyApi.insertjxbjopticallog({
							workPlanId: this.work.id,
							consultationDeductionId: this.work.OBJID,
							tFlowContentId: this.work.flowId,
							array: CheckedAllArr
						}).then(res => {
							Toast(res.msg)
							this.back();
						}).catch(error => {
							Toast("提交失败")
						})
					})
					.catch(() => {
						// on cancel
					});
			},
		},
	}
</script>

<style lang="scss" scoped>
	.maintenance {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		background: #F2F6FA;
		overflow: hidden;
		box-sizing: border-box;

		.main {
			flex: 1;
			width: 100vw;
			overflow: scroll;
			padding: px2vh(30) px2vw(30) 0;
			box-sizing: border-box;

			.clickIcon {
				width: 13px;
				height: 13px;
			}

			text {
				font-size: px2vh(40);
				font-weight: bold;
				color: #666666;
			}

			.layout {
				display: flex;
				align-items: center;
			}

			.title {
				margin-bottom: px2vh(30);

				text:nth-child(1) {
					flex: 1;
				}

				text:last-child {
					color: #98999B;
				}

				.active {
					color: #417ADC !important;
				}

				img {
					margin-right: px2vw(16);
				}
			}

			.content-list {
				width: px2vw(1020);
				background: #FFFFFF;
				margin-bottom: px2vh(30);
				border-radius: px2vh(20);

				.border {
					margin: auto px2vw(50);
					padding: px2vh(50) 0;
				}

				.list-top {
					text {
						margin-right: auto;
						margin-left: px2vh(18);
					}
				}

				.list-bottom {
					.details {
						height: px2vh(74);

						text {
							flex: 1;
							font-size: px2vh(42);
							font-weight: normal;
						}
					}
				}
			}
		}

		.verify {
			width: 100%;
			padding: px2vh(30);
			box-sizing: border-box;

			.van-button--normal {
				width: 100%;
				height: px2vh(120);
				font-size: px2vh(46);
				background: #417ADC;
				border: 1px solid #417ADC;
			}
		}
	}

	.img-icon {
		width: 13px;
		height: 13px;
	}
</style>
